<?php
namespace Controller;
use Kuyuan\WqCore\controller\Controller;
use Kuyuan\WqCore\util\DB;
use Model\MemberCouponModel;

/**
 * 红包记录
 * Class Red
 * @package Controller
 */
class MemberCoupon extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 新增用户卡券
     * @param array $_data 数据
     * @return mixed
     */
    public function add(Array $_data)
    {
        $_data["uid"] = $this->_uid;
        $_data["uniacid"] = $this->_uniacid;
        return MemberCouponModel::create($_data);
    }

    /**
     * 通过红包id和uid返回一个红包开到的卡券信息
     * @param integer $_red_id 红包id
     * @return mixed
     */
    public function find($_red_id)
    {
        return MemberCouponModel::where("red_id",$_red_id)
            ->where("uid",$this->_uid)
            ->field(array("id","coupon_id","indate","status"))
            ->cache(!KUYUAN_DEBUG)
            ->find();
    }

    /**
     * 获取一个用户的可用卡券数量
     * @param int $_uid
     * @return mixed
     */
    public function getUserNumber($_uid = 0)
    {
        if(!$_uid) $_uid = $this->_uid;
        return MemberCouponModel::where("uid",$_uid)
            ->where("status",1)
            ->where("indate",1)
            ->whereOr("indate",">",TIMESTAMP)
            ->count();
    }

    /**
     * 获取指定用户卡券列表
     * @param string $_type 卡券类型
     * @param array $_limit
     * @param int $_uid
     * @return mixed
     */
    public function getUserList($_type,Array $_limit,$_uid = 0)
    {
        if(!$_uid) $_uid = $this->_uid;
        $_bind = array("uid"=>$_uid);
        $_sql = "SELECT 
                        a.id,a.coupon_id,a.status,a.indate,b.type
                 FROM
                        ".DB::tableName("member_coupon")." AS a
                 LEFT JOIN
                        ".DB::tableName("coupon")." AS b
                 ON
                    a.coupon_id = b.id
                 WHERE
                    a.uid = :uid
                 AND
                    a.status < 9";
        if($_type != 'all') {
            $_sql .= " AND b.type = :type";
            $_bind["type"] = $_type;
        }
        $_sql .= " ORDER BY
                    a.status ASC,a.indate = 1 OR a.indate >= ".TIMESTAMP." DESC,a.time DESC
                    LIMIT ".$_limit[0].",".$_limit[1]."";
        return MemberCouponModel::sql($_sql)
            ->bind($_bind)
            ->cache(!KUYUAN_DEBUG)
            ->fetchAll();
    }

    /**
     * 删除用户卡券
     * @param $_member_coupon_id
     * @return mixed
     */
    public function del($_member_coupon_id)
    {
        return MemberCouponModel::delete($_member_coupon_id);
    }

    /**
     * 获取卡券详情
     * @param $_member_coupon_id
     * @return mixed
     */
    public function get($_member_coupon_id)
    {
        return MemberCouponModel::where("id",$_member_coupon_id)
            ->where("status","<",9)
            ->field(array("id","status","coupon_id","indate"))
            ->cache(!KUYUAN_DEBUG)
            ->find();
    }

    /**
     * 修改数据
     * @param $_data
     * @param $_member_coupon_id
     * @return mixed
     */
    public function edit($_data,$_member_coupon_id)
    {
        return MemberCouponModel::where("id",$_member_coupon_id)
            ->update($_data);
    }
}